package com.example.wechat.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.wechat.R;
import com.example.wechat.model.MeMenuItem;
import java.util.List;

public class MeMenuAdapter extends RecyclerView.Adapter<MeMenuAdapter.ViewHolder> {
    private List<MeMenuItem> menuItems;
    private OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public MeMenuAdapter(List<MeMenuItem> menuItems) {
        this.menuItems = menuItems;
    }

    public void setMenuItems(List<MeMenuItem> menuItems) {
        this.menuItems = menuItems;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_me_menu, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        MeMenuItem item = menuItems.get(position);
        holder.iconView.setImageResource(item.getIconResId());
        holder.titleView.setText(item.getTitle());
        holder.subtitleView.setText(item.getSubtitle());
        holder.subtitleView.setVisibility(item.getSubtitle().isEmpty() ? View.GONE : View.VISIBLE);
        
        if (item.getBadge() > 0) {
            holder.badgeView.setVisibility(View.VISIBLE);
            holder.badgeView.setText(String.valueOf(item.getBadge()));
        } else {
            holder.badgeView.setVisibility(View.GONE);
        }

        holder.itemView.setOnClickListener(v -> {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return menuItems.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iconView;
        TextView titleView;
        TextView subtitleView;
        TextView badgeView;

        ViewHolder(View view) {
            super(view);
            iconView = view.findViewById(R.id.menu_icon);
            titleView = view.findViewById(R.id.menu_title);
            subtitleView = view.findViewById(R.id.menu_subtitle);
            badgeView = view.findViewById(R.id.menu_badge);
        }
    }
} 